import { Component, OnInit } from '@angular/core';
import * as _ from 'lodash';
import { PublicRemouldService } from './remould.service';
import { environment } from '../../../../environments/environment';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { Util } from '../../../common/util';
@Component({
  selector: 'app-remould',
  templateUrl: './remould.component.html',
  styleUrls: ['./remould.component.scss'],
  providers: [PublicRemouldService]
})
export class RemouldComponent implements OnInit {

  // 列表数据
  public listData = {
    list: []
  };

  // 列表是否全选
  public isCheckedAll = false;
  // 字典数据
  public dictionaryList = {
    CHANGE_TYPE: []
  };

  //产品改造分页配置
  public remouldPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  public postBody = {
    // token: 1,
    sFullName: '',
    sFundCode: '',
    sChageType: '',
    sStatus: '',
    pageIndex: 1,
    pageSize: 10

  }
  // list每一项的集合，用于实现全选等功能
  productList: any = [];

  constructor(
    public publicRemouldService: PublicRemouldService
  ) { }

  ngOnInit() {
    this.loadlist();
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
  }

  /**
* 改变产品改造列表显示记录数
* @param {pageSize}
*/
  public bondsManpageSizeChange(pageSize: number) {
    if (pageSize !== this.remouldPageInfo.pageSize) {
      this.remouldPageInfo.pageSize = pageSize;
      // this.bondsManpageInfo.currentPageNum = 1;
      this.loadlist();
    }
  }
  //产品改造管理分页
  public bondsManPageNavigation(currentPageNum: number) {
    this.remouldPageInfo.currentPageNum = currentPageNum;
    this.loadlist(this.remouldPageInfo.currentPageNum,
      this.remouldPageInfo.pageSize)
  }
  //查询分页列表
  loadlist(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.postBody.sFullName = _.trim(this.postBody.sFullName);
    this.postBody.sFundCode = _.trim(this.postBody.sFundCode);  
    this.postBody.pageIndex = pageNum ? pageNum : this.remouldPageInfo.currentPageNum;
    this.postBody.pageSize = pageSize ? pageSize : this.remouldPageInfo.pageSize;
    this.publicRemouldService.list(this.postBody).subscribe(data => {
      if (data) {
        that.listData = data;
        that.remouldPageInfo.currentPageNum = data.pageNum;
        that.remouldPageInfo.totalPages = data.pages;
        that.remouldPageInfo.total = data.total; // 总记录数
        that.remouldPageInfo.startRow = data.startRow;
        that.remouldPageInfo.endRow = data.endRow;
      }
      //  // 给表格列支持列拖动效果
      //  setTimeout(() => {
      //   Util.colResizable("#fixed-table");
      // }, 100);

      //给表格列支持列拖动和固定表头效果
      Util.colResizable("#remouldList");
    })
  }
  /**
 * 列表全选
 * @param status 
 */
  checkedAll() {
    if (this.isCheckedAll) { // 更新为全选
      _.forEach(this.productList, item => {
        item.isChecked = true;


      });
    } else { // 更新为不全选
      _.forEach(this.productList, item => {
        item.isChecked = false;
      });
    }
  }
      // 点击表格头排序
      orderData(colName) {
        let listDataAsc = _.orderBy(this.listData.list, [colName], ["asc"]);
        if (JSON.stringify(this.listData.list) == JSON.stringify(listDataAsc)) {
          listDataAsc = _.orderBy(this.listData.list, [colName], ["desc"]);
        }
        this.listData.list = listDataAsc;
      }
}
